<template>
  <div class="box">
    <el-input
      class="input__inner"
      placeholder="请输入部门名称"
      v-model="filterText"
    >
    </el-input>

    <el-tree
      class="filter-tree"
      :data="data"
      :props="defaultProps"
      default-expand-all
      :filter-node-method="filterNode"
      ref="tree"
    >
    </el-tree>
  </div>
</template>

<script>
export default {
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },

  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
  },

  data() {
    return {
      filterText: "",
      data: [
        {
          id: 1,
          label: "诺一科技",
          children: [
            {
              id: 2,
              label: "深圳总公司",
              children: [
                {
                  id: 4,
                  label: "研发部",
                },
                {
                  id: 5,
                  label: "市场部",
                },
                {
                  id: 6,
                  label: "测试部",
                },
                {
                  id: 7,
                  label: "财务部",
                },
                {
                  id: 8,
                  label: "运维部",
                },
              ],
            },
            {
              id: 3,
              label: "长沙分公司",
              children: [
                {
                  id: 9,
                  label: "市场部",
                },
                {
                  id: 10,
                  label: "财务部",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
};
</script>

<style lang="scss" scoped>
// .box {
//   margin-top: 20px;
// }
// .input__inner {
//   width: 15%;
// }

// .filter-tree {
//   width: 15%;
// }
</style>